package com.javajavadog.ui;

import android.app.AlertDialog;
import android.app.Fragment;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.javajavadog.ui.base.FragmentHostActivity;

public class ControlsBasicActivity extends FragmentHostActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public String getTitleText() {
        return "Basic Controls";
    }

    @Override
    public Fragment getContentFragment() {
        return ControlsBasicFragment.newInstance();
    }

    @Override
    public boolean enableBackButton() {
        return true;
    }

    public static class ControlsBasicFragment extends Fragment {

        public static ControlsBasicFragment newInstance() {
            ControlsBasicFragment f = new ControlsBasicFragment();
            return f;
        }

        public ControlsBasicFragment() {
            // empty
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_ui_controls, null);
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
            // Create an ArrayAdapter using the string array and a default spinner layout
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
                    R.array.planets_array, android.R.layout.simple_spinner_item);
            // Specify the layout to use when the list of choices appears
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            // Apply the adapter to the spinner
            spinner.setAdapter(adapter);

            view.findViewById(R.id.button_show_dialog_basic).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder b = new AlertDialog.Builder(getActivity())
                            .setTitle("Alert")
                            .setMessage("Show the message to user");
                    b.show();
                }
            });
            view.findViewById(R.id.button_show_dialog_simple_button).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder b = new AlertDialog.Builder(getActivity())
                            .setTitle("Alert")
                            .setMessage("Show the message to user");
                    b.setPositiveButton("OK", null);
                    b.setCancelable(false);
                    b.show();
                }
            });
            view.findViewById(R.id.button_show_dialog_full_button).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder b = new AlertDialog.Builder(getActivity())
                            .setTitle("Alert")
                            .setMessage("Show the message to user");
                    b.setPositiveButton("Positive", null);
                    b.setNeutralButton("Neutral", null);
                    b.setNegativeButton("Negative", null);
                    b.setCancelable(false);
                    b.show();
                }
            });
            view.findViewById(R.id.button_show_dialog_progress).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showProgressDialog("Loading, please wait...");
                }
            });

            view.findViewById(R.id.button_show_popup_menu).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PopupMenu menu = new PopupMenu(getActivity(), v);
                    menu.getMenu().add(0, 0, 0, "Item One");
                    menu.getMenu().add(0, 1, 0, "Item Two");
                    menu.getMenu().add(0, 2, 0, "Item Three");
                    menu.getMenu().add(0, 3, 0, "Item Four");
                    menu.getMenu().add(0, 4, 0, "Item Five");
                    menu.show();
                }
            });
            view.findViewById(R.id.button_show_popup_window).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TextView tv = new TextView(getActivity());
                    tv.setText("Hello popup window");

                    PopupWindow popupWindow = new PopupWindow(getActivity());
                    popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                    popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                    popupWindow.setContentView(tv);
                    popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
                    popupWindow.setOutsideTouchable(false);
                    popupWindow.setFocusable(true);
                    popupWindow.showAsDropDown(v, 0, 0, Gravity.RIGHT);
                }
            });
        }

        public void showProgressDialog(String message) {
            View view = createProgressView(message);
            AlertDialog.Builder b = new AlertDialog.Builder(getActivity()).setView(view);
            b.show();
        }

        private View createProgressView(String message) {
            LinearLayout view = new LinearLayout(getActivity());
            view.setOrientation(LinearLayout.VERTICAL);

            ProgressBar pb = new ProgressBar(getActivity());
            int p16 = getPaddingPixel();
            int p32 = 2 * getPaddingPixel();
            pb.setPadding(p16, p32, p16, p16);
            TextView text = new TextView(getActivity());
            text.setText(message);
            text.setPadding(p16, p16, p16, p32);

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.CENTER;

            view.addView(pb, layoutParams);
            view.addView(text, layoutParams);

            return view;
        }

        public int getPaddingPixel() {
            int p = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics());
            return p;
        }
    }
}
